﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System;

namespace WebClient.CURD
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            previewImg.Source = new BitmapImage(new Uri(@"C:\\Users\\NINTH\\Desktop\\TIM图片20190628092159.jpg", UriKind.Relative));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog myOpenFileDialog = new OpenFileDialog();
            myOpenFileDialog.ShowDialog();
            var filePath = myOpenFileDialog.FileName;
            //定义_webClient对象
            System.Net.WebClient _webClient = new WebClient
            {
                //使用Windows登录方式
                Credentials = new NetworkCredential("File", "f123")
            };

            //上传的链接地址（文件服务器）

            //Uri _uri = new Uri(@"http(s)://upload-z2.qiniup.com" + myOpenFileDialog.FileName.Remove(0, myOpenFileDialog.FileName.ToString().LastIndexOf(@"\")));
            Uri _uri = new Uri(@"http://120.77.3.221:8084/UploadFiles/" + myOpenFileDialog.FileName.Remove(0, myOpenFileDialog.FileName.ToString().LastIndexOf(@"\")));
            //要传到指定文件夹则URL为：@"http://192.168.1.252:8010//文件夹名字"+"文件名"

            //注册上传进度事件通知
            _webClient.UploadProgressChanged += _webClient_UploadProgressChanged;

            //注册上传完成事件通知
            _webClient.UploadFileCompleted += _webClient_UploadFileCompleted;

            //异步从D盘上传文件到服务器
            _webClient.UploadFileAsync(_uri, "PUT", myOpenFileDialog.FileName);
        }
        //下载进度事件处理程序
        private void _webClient_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
        {
            //if (e.ProgressPercentage == 0)
            //{
            //    timer_Tick(null, null);
            //    // MessageBox.Show($"{e.ProgressPercentage}:{e.BytesSent}/{e.TotalBytesToSend}");
            //}
        }
        //下载完成事件处理程序
        private void _webClient_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
        {
            //progressBar1.Value = 100;
            //MessageBox.Show("上传成功");
            //timer1.Stop();
            //progressBar1.Value = 0;
        }
    }
}
